package com.changgou.order.controller;

import com.changgou.order.config.TokenDecode;
import com.changgou.order.pojo.OrderItem;
import com.changgou.order.service.CartService;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author zhengjd
 * @date 2020/5/17 22:49
 */
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    @Autowired
    private TokenDecode tokenDecode;

    /**
     * 添加购物车
     * @param num 添加数量
     * @param id  skuId
     * @return
     */
    @RequestMapping("/add")
    public Result add(Integer num,Long id){
        //获取用户名
        String username = tokenDecode.getUsername();
        //将商品添加到Redis中
        cartService.add(num,id,username);
        return new Result(true, StatusCode.OK,"商品添加购物车成功");
    }

    @RequestMapping("/list")
    public Result<List<OrderItem>> list(){
        //获取用户名
        String username = tokenDecode.getUsername();
        List<OrderItem> orderItems = cartService.list(username);
        return new Result<>(true,StatusCode.OK,"查询购物车列表成功",orderItems);
    }
}
